//
//  WSDiscoverRequest.m
//  CharonTVProject
//
//  Created by karong on 2023/7/31.
//

#import "WSDiscoverRequest.h"
#import "WSChannel.h"
#import "WSChannelItem.h"
#import "WSVideo.h"

@implementation WSDiscoverRequest

+ (void)loadcharonTV_HomeChannelSuccess:(charonTV_ArrayBlock)success
                                 failed:(Failure)failed {
    NSString * charonTV_url = API_CHARONTV_CHANNEL;
    NSDictionary * charonTV_parameters = @{};
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSMutableArray * charonTV_channelList = [WSChannel mj_objectArrayWithKeyValuesArray:responseDict[@"items"]];
        success(charonTV_channelList);
    } failure:failed];
}

+ (void)loadcharonTV_ChannelListWithChannelId:(NSInteger)charonTV_channelId channelType:(NSInteger)charonTV_channelType success:(nonnull charonTV_ArrayBlock)success failed:(nonnull Failure)failed {
    NSString * charonTV_url = API_CHARONTV_CHANNEL_CONFIG;
    NSDictionary * charonTV_parameters = @{@"channel_id":@(charonTV_channelId),
                                           @"channel_type":@(charonTV_channelType)};
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSDictionary *typeDict = @{@(WSChannelConfigType1): @"WSBook", /// 横向布局一行3个
                                   @(WSChannelConfigType2): @"WSBook", /// 横向布局一行3个
                                   @(WSChannelConfigType3): @"WSBook", /// 横向布局1大帶多小
                                   @(WSChannelConfigType4): @"WSBook", /// 横向布局一行3个
                                   @(WSChannelConfigType5): @"WSBook", /// 排行榜 一行两列*榜单数量
                                   @(WSChannelConfigType6): @"WSBook", /// 横向布局水平滑動
                                   @(WSChannelConfigType7): @"WSBook", /// 列表样式
                                   @(WSChannelConfigType8): @"WSBook", /// 横向布局1大帶横向水平滑動
                                   @(WSChannelConfigType9): @"WSBook", /// 横向布局水平滑動
                                   @(WSChannelConfigType10) : @"WSItem", /// 分类 横向一行2个
                                   @(WSChannelConfigType11) : @"WSItem", /// 运营位小喇叭
                                   @(WSChannelConfigType12) : @"WSItem", /// banner（656*300）
                                   @(WSChannelConfigType13) : @"WSItem", /// banner（656*120）
                                   @(WSChannelConfigType14) : @"WSBook", /// 横向一行3个 *右上角标簽
                                   @(WSChannelConfigType15) : @"WSBook", /// 排行榜 一行两列*榜单数量
                                   @(WSChannelConfigType16) : @"WSItem", /// 金刚区
                                   @(WSChannelConfigType17) : @"WSBook", /// 横向一行3个 *左上角标簽
                                   @(WSChannelConfigType18) : @"WSBook", /// 纵向布局 帶排行 分数
                                   @(WSChannelConfigType19) : @"WSBook", /// 纵向布局 帶排行
                                   @(WSChannelConfigType20) : @"WSBook", /// 横向布局一行3个
                                   @(WSChannelConfigType21) : @"WSBook", /// 横向布局一行3个
        };
        NSArray * array = [WSChannelItem mj_objectArrayWithKeyValuesArray:responseDict[@"items"]];
        NSMutableArray * channelItemArray = [NSMutableArray new];
        for (WSChannelItem * channelItem in array) {
            if (channelItem.items.count > 0) {
                NSString *className = typeDict[@(channelItem.style)];
                if (className.length == 0) {
                    continue;
                }
                Class class = NSClassFromString(className);
                channelItem.items = [class mj_objectArrayWithKeyValuesArray:channelItem.items];
                [channelItemArray addObject:channelItem];
            }
        }
        success(channelItemArray);
    } failure:failed];
}

+ (void)loadcharonTV_ForYouListWithChannel_id:(NSInteger)channel_id page:(NSInteger)page success:(charonTV_ArrayBlock)success failed:(Failure)failed {
    NSString * charonTV_url = API_CHARONTV_FOR_YOU_CHOICE;
    NSDictionary * charonTV_parameters = @{@"channel_id":@(channel_id),
                                           @"page":@(page)
    };
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSMutableArray * charonTV_channelList = [WSVideo mj_objectArrayWithKeyValuesArray:responseDict[@"items"]];
        success(charonTV_channelList);
    } failure:failed];
}
#pragma mark - PagableRequestPotocol
- (void)refreshDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading) {
        return;
    }
    self.loading = YES;
    self.page = 1;
    self.haveMore = YES;
    [WSDiscoverRequest loadcharonTV_ForYouListWithChannel_id:self.charonTV_channelId page:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}


- (void)loadMoreDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading && !self.haveMore) {
        return;
    }
    [WSDiscoverRequest loadcharonTV_ForYouListWithChannel_id:self.charonTV_channelId page:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadSuccess:(charonTV_ArrayBlock)success withArray:(NSArray *)array {
    self.haveMore = array.count >= 10;
    self.page++;
    self.loading = NO;
    success(array);
}
@end
